var CommSlide = new Class({

	initialize: function(params){
		this.items = params.items;
		this.mode = params.mode || 'horizontal';
		this.modes = {horizontal:['margin-left', 'width'], vertical:['margin-top', 'height']};
		this.size = params.size || 984;
		this.box = params.box.setStyle(this.modes[this.mode][1], (this.size * this.items.length) + 'px');
		this.button_event = 'click';
		this.handle_event = 'click';
		this.onWalk = params.onWalk || null;
		this.currentIndex = null;
		this.interval = params.interval || 5000;
		this.handles = params.handles || null;
		if(this.handles){
			this.addHandleButtons(this.handles);
		}
		this.buttons = {
			previous: [],
			next: []
		};
		if(params.addButtons){
			for(var action in params.addButtons){
				this.addActionButtons(action, $type(params.addButtons[action])=='array' ? params.addButtons[action] : [params.addButtons[action]]);
			}
		}
		this.fx = new Fx.Tween(this.box,$extend((params.fxOptions||{duration:500,wait:false}),{property:this.modes[this.mode][0]}));
		this.walk(0,true,true);
	},

	addHandleButtons: function(handles){
		for(var i=0;i<handles.length;i++){
			handles[i].addEvent(this.handle_event,this.walk.bind(this,[i,true]));
		}
	},
	
	addActionButtons: function(action,buttons){
		for(var i=0; i<buttons.length; i++){
			switch(action){
				case 'previous': buttons[i].addEvent(this.button_event,this.previous.bind(this,[true])); break;
				case 'next': buttons[i].addEvent(this.button_event,this.next.bind(this,[true])); break;
			}
			this.buttons[action].push(buttons[i]);
		}
	},

	previous: function(manual){
		if (this.currentIndex > 0) {
			this.walk(this.currentIndex - 1, manual);
		}
	},

	next: function(manual){
		if (this.currentIndex < this.items.length-1) {
			this.walk(this.currentIndex + 1, manual);
		}
	},

	walk: function(item,manual,noFx){
		if(item!=this.currentIndex){
			this.currentIndex = item;
			if(manual){
				this.fx.cancel();
			}
			if(noFx){
				this.fx.cancel().set((this.size*-this.currentIndex)+'px');
			}else{
				this.fx.start(this.size*-this.currentIndex);
			}
			if(this.onWalk){
				this.onWalk((this.items[this.currentIndex] || null), (this.handles && this.handles[this.currentIndex] ? this.handles[this.currentIndex] : null));
			}
		}
	}
	
});